package com.smstransmit.service

import android.annotation.TargetApi
import android.app.Notification
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import com.smstransmit.service.SmsService.LocalBinder
import android.app.PendingIntent
import android.content.IntentFilter
import android.graphics.BitmapFactory
import android.os.Build
import com.smstransmit.MainActivity
import com.smstransmit.R
import com.smstransmit.reciver.SmsReciver
import com.xmrecruit.CC


/**
 * Created by shijie.guo on 2017/9/11.
 */
class SmsService : Service() {
    private val mBinder = LocalBinder()
    var reciver: SmsReciver? = null

    override fun onBind(p0: Intent?): IBinder {
        return mBinder
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    override fun onCreate() {
        super.onCreate()

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//        reciver = SmsReciver()
//        val filter = IntentFilter()
//        filter.addAction("android.provider.Telephony.SMS_RECEIVED")
//        registerReceiver(reciver, filter)

        var builder = Notification.Builder(CC.applicationContext)
        val intent = Intent(CC.applicationContext, MainActivity::class.java)
//        builder.setContentIntent(PendingIntent.getActivity(CC.applicationContext, 0, intent, 0))
//        builder.setLargeIcon(BitmapFactory.decodeResource(CC.applicationContext?.resources, R.mipmap.ic_launcher_round))
        builder.setSmallIcon(R.mipmap.ic_launcher_round)
        builder.setContentText("短信转发服务正在运行中")
        builder.setContentTitle("短信转发")
        builder.setWhen(System.currentTimeMillis())
        val notification = builder.build()
        startForeground(121290, notification)
        return super.onStartCommand(intent, flags, startId)

    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(reciver)
        stopForeground(true)// 停止前台服务--参数：表示是否移除之前的通知
    }

    inner class LocalBinder : Binder() {
        fun getService(): SmsService {
            return this@SmsService
        }
    }
}